<?php
// +----------------------------------------------------------------------------
// | Tke团队版权所属 [三十年河东三十年河西,莫欺少年穷.!]
// +----------------------------------------------------------------------------
// | Copyright (c) 2014 http://www.itdaodan.com All rights reserved.
// +----------------------------------------------------------------------------
// | Author: Cruise<jiangyu@hrbjjhh.com>
// +----------------------------------------------------------------------------

namespace Admin\Model;
use Admin\Model\BaseModel;
/**
 * @todo 商品品类型模型
 * @author Cruise <jiangyu@hrbjjhh.com>
 */
class ShopGoodsCategoryModel extends BaseModel {

    /* 自动验证规则 */
    protected $_validate = array(
    	array('goods_category_name', 'require', '请输入商品品类', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
    	array('goods_category_name', 'checkName', '商品品类已经存在', self::MUST_VALIDATE, 'callback', self::MODEL_BOTH),
    	array('goods_category_name', '1,10', '商品品类不能超过10个字符', self::MUST_VALIDATE, 'length', self::MODEL_BOTH),
    	array('sort', 'number', '排序必须是数字', self::MUST_VALIDATE, '', self::MODEL_BOTH),
    );

    /* 自动完成规则 */
    protected $_auto = array(
    	array('create_time', NOW_TIME, self::MODEL_INSERT),
    	array('update_time', NOW_TIME, self::MODEL_UPDATE),
    );

    /**
     * @todo 检测名称是否存在
     * @param  string   $goods_category_name 商品品类名称
     * @return boolean     ture - 未重名，false - 已重名
     * @author Cruise <jiangyu@hrbjjhh.com>
     */
    protected function checkName($goods_category_name){
    	$goods_category_id = (int)I('goods_category_id');
    	$goods_category_pid = (int)I('goods_category_pid');
    	//有上级的情形
    	if($goods_category_pid > 0){
    		$map['goods_category_pid'] = $goods_category_pid;
    	}else{
    		$map['goods_category_pid'] = 0;
    	}
    	$map['goods_category_name'] = $goods_category_name;
    	$map['goods_category_id'] = array('neq',$goods_category_id);
    	$map['status'] = array('gt',-1);
    	if ($this->where($map)->field('goods_category_id')->find()){ //fang update
    		return false;
    	}
    	 return true;
    }
    
    /**
     * @todo 商品一级品类详情
     * @param int $goods_category_id 商品一级品类id
     * @param string $field 查询字段
     * @return false|array
     * @author Cruise <jiangyu@hrbjjhh.com>
     */
    public function GoodsCategoryShow($goods_category_id = 0,$field = true){//fang update
    	//获取数据
    	$goods_category_id = (int)$goods_category_id;
    	if($goods_category_id == 0){
    		$this->error = '请正确操作';
    		return false;
    	}
    	$map = array();//初始化查询条件
    	//查询条件
    	$map['goods_category_id'] = $goods_category_id;
    	//返回值
    	return $this->where($map)->field($field)->find();
    	
    }
    
    /**
     * @todo 取出一级经营品类
     * @param string $field 查询字段
     * @return array
     * @author Cruise <jiangyu@hrbjjhh.com>
     */
    public function firstGoodsCategoryShow($field = true){
    	//获取数据
    	$map['goods_category_pid'] = 0;
    	$map['status'] = array('gt',-1);
    	$first_goods_gategory = $this->where($map)->field($field)->select();
    	if(empty($first_goods_gategory)){
    		return array();  //没有数据返回空数组
    	}
    	return $first_goods_gategory;
    }
    
    /**
     * @todo 判断一级品类下是否有二级品类 删除验证
     * @param string $goods_category_id
     * @return boolean false 没有下级品类，true 存在下级品类
     * @author Cruise <jiangyu@hrbjjhh.com>
     */
    public function judgeGoodsCategory($goods_category_id=''){
    	if($goods_category_id == ''){
    		$this->error = '参数丢失，请重试';
    		return false;
    	}
    	$map['status'] = array('gt',-1);
    	$map['goods_category_pid'] = array('in',$goods_category_id);
    	$data = $this->where($map)->field('goods_category_id')->find();
    	if($data){
    		return true;
    	}else{
    		return false;
    	}
    }
    
    /**
     * @todo 返回主键
     * @see \Think\Model::getPk()
     * @author PHP@妖孽 <mashengjie@hrbjjhh.com>
     */
    public function getPk(){
    	return 'goods_category_id';
    }
}
